home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / extmath.zip / DIVTEST.C next >
C/C++ Source or Header  |  1993-01-07  |  3KB  |  95 lines

  1. /*
  2.     divtest.c: time and test 64-bit extended precision arithmetic library
  3.  
  4.     This program generates 1024 64-bit unsigned numbers, then uses them to
  5.     compare two different divide algorithms. On a generic 12MHz 286 clone,
  6.     the results are:
  7.  
  8.     ExtDivB: average time = 4.292582e-04 seconds
  9.     ExtDiv: average time = 4.829155e-05 seconds
  10.  
  11.     Note: If preprocessor symbol CHECK_RES is uncommented, the results of
  12.     the division ops will be checked with a multiply, an add, and a compare.
  13.     It should be commented out for time tests.
  14.  
  15.     Written and tested with Borland Turbo C++ 1.0, small model. Link with
  16.     "extmath.lib".
  17.  
  18.     Tim Victor, January 7, 1993
  19. */
  20.  
  21.  
  22. #include <time.h>
  23. #include "extmath.h"
  24.  
  25. extnum_t dividend[1024], divisor[1024];
  26.  
  27. #define ITERATIONS 10240
  28. //#define CHECK_RES
  29.  
  30. main()
  31. {
  32.     int i, j, x, res;
  33.     extnum_t quotient, remainder;
  34.     clock_t start, end;
  35.  
  36.     /* initialize values */
  37.     for (j=0; j<1024; ++j) {
  38.         for (i=0; i<4; ++i) {
  39.             dividend[j][i] = rand();
  40.             divisor[j][i] = rand();
  41.         }
  42.     }
  43.  
  44.     /* do ops w/ obvious (slow) divide algorithm */
  45.     start = clock();
  46.     x = 0;
  47.     for (j=0; j<ITERATIONS; ++j) {
  48. #ifdef CHECK_RES
  49.         res = ExtDivB(dividend[x], divisor[x], quotient, remainder);
  50.         if (res)
  51.             printf("%d, Divide By Zero error\n", j);
  52.         else {
  53.             ExtMul(divisor[x], quotient);
  54.             ExtAdd(remainder, quotient);
  55.             if (ExtCmp(dividend[x], quotient))
  56.                 printf("%d, Unequal result\n", j);
  57.         }
  58. #else
  59.         ExtDivB(dividend[x], divisor[x], quotient, remainder);
  60. #endif
  61.         if (++x >= 1024)
  62.             x = 0;
  63.     }
  64.     end = clock();
  65.     printf("ExtDivB: average time = %e seconds\n",
  66.         (end-start)/(ITERATIONS*CLK_TCK));
  67.  
  68.  
  69.     /* do ops w/ bit-matching divide algorithm */
  70.     start = clock();
  71.     x = 0;
  72.     for (j=0; j<ITERATIONS; ++j) {
  73. #ifdef CHECK_RES
  74.         res = ExtDiv(dividend[x], divisor[x], quotient, remainder);
  75.         if (res)
  76.             printf("%d, Divide By Zero error\n", j);
  77.         else {
  78.             ExtMul(divisor[x], quotient);
  79.             ExtAdd(remainder, quotient);
  80.             if (ExtCmp(dividend[x], quotient))
  81.                 printf("%d, Unequal result\n", j);
  82.         }
  83. #else
  84.         ExtDiv(dividend[x], divisor[x], quotient, remainder);
  85. #endif
  86.         if (++x >= 1024)
  87.             x = 0;
  88.     }
  89.     end = clock();
  90.     printf("ExtDiv: average time = %e seconds\n",
  91.         (end-start)/(ITERATIONS*CLK_TCK));
  92.  
  93.     return(0);
  94. }
  95.